package com.tz.farmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.mapper.*;
import com.tz.farmer.model.*;
import com.tz.farmer.service.BusinessService;
import com.tz.farmer.service.GoodsInfoService;
import com.tz.farmer.so.GoodsSo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoMapper, GoodsInfo> implements GoodsInfoService {

    Logger logger = LoggerFactory.getLogger(GoodsInfoServiceImpl.class);
    @Autowired
    GoodsInfoMapper goodsInfoMapper;
    @Autowired
    MainBodyInfoMapper mainBodyInfoMapper;
    @Autowired
    BusinessInfoMapper businessInfoMapper;
    @Autowired
    OperateInfoMapper operateInfoMapper;
    @Autowired
    TradeInfoMapper tradeInfoMapper;
    @Override
    public ResultEx saveGoodsInfo(List<GoodsInfo> list) throws Exception{
        //批量插入商品数据
        saveBatch(list);
        return ResultEx.builder().makeResult(200, "成功", "");
    }

    @Override
    public ResultEx getGoodsInfo(GoodsInfo goodsInfo) throws Exception{
        Map map=new HashMap();
        logger.info("--------"+goodsInfo.getGoodsId()+goodsInfo.getBatchInfo()+goodsInfo.getUniscId());
        GoodsInfo goods=goodsInfoMapper.selectOneGoods(goodsInfo.getGoodsId(),goodsInfo.getBatchInfo());
        GoodsInfo g = new GoodsInfo();
        g.setGoodsName(goods.getGoodsName());
        g.setAmount(goods.getAmount());
        g.setResult(goods.getResult());
        g.setSpecs(goods.getSpecs());
        g.setBrand(goods.getBrand());
        g.setGoodsPhoto(goods.getGoodsPhoto());
        map.put("goodsInfo",g);
        if("大黄鱼".equals(goods.getGoodsName())){
            //新增环节
            Map addLink = operateInfoMapper.selectAddLink(goodsInfo.getUniscId());
            map.put("addLink",addLink);
        }


        logger.info("开始查询交易信息===========================================");
        TradeInfo tradeInfo = tradeInfoMapper.selectTradeInfo(goods.getGoodsName(),goods.getTradeTime());
        if(tradeInfo == null){
            map.put("mainBodyInfo",null);
            map.put("mainProductInfo",null);
            map.put("mainAuthInfo",null);
            map.put("mainDetectInfo",null);
            map.put("operateList",null);
            map.put("quickScan",null);
            return ResultEx.builder().makeSuccessResult(map);
        }


        logger.info("产品id===================================="+tradeInfo.getPInfoId());
        //产品信息
        DetailsVo mainProductInfo= businessInfoMapper.selectMainProductInfo(tradeInfo.getPInfoId());
        //主体信息
        DetailsVo mainBodyInfo= businessInfoMapper.selectMainBodyInfo(mainProductInfo.getLeadId());

        //认证信息
        DetailsVo mainAuthInfo= businessInfoMapper.selectMainAuthInfo(goods.getGoodsName(),mainBodyInfo.getId());

        //检测信息
        List<DetailsVo> mainDetectInfo= businessInfoMapper.selectMainDetectInfo(mainProductInfo.getId());

        //种植信息
        List<OperateInfoVo> operateList = operateInfoMapper.selectOperateList(mainProductInfo.getId(),mainProductInfo.getPName());
        logger.info("种植信息条数++++++++++++++++++++++++++++"+operateList.size());

        //快检信息
        Map<String,Object> quickScan=operateInfoMapper.selectQuickScan(11556);
        map.put("quickScan",quickScan);
        map.put("mainBodyInfo",mainBodyInfo);
        map.put("mainProductInfo",mainProductInfo);
        map.put("mainAuthInfo",mainAuthInfo);
        map.put("mainDetectInfo",mainDetectInfo);
        map.put("operateList",operateList);
        return ResultEx.builder().makeSuccessResult(map);
    }

    @Override
    public ResultEx getFishGoodsInfo(GoodsInfo goodsInfo) throws Exception{
        Map map=new HashMap();
        //商品基本信息
        GoodsInfo goods=goodsInfoMapper.selectBoatDetail(goodsInfo.getGoodsId(),goodsInfo.getBatchInfo());
        map.put("goodsInfo",goods);
        //快检信息
        Map<String,Object> quickScan=operateInfoMapper.selectQuickScan(83);
        map.put("quickScan",quickScan);

        return ResultEx.builder().makeSuccessResult(map);
    }

    @Override
    public GoodsInfo getBoatDetail(String goodsId, String uniscId, String batchInfo) {
        return goodsInfoMapper.selectBoatDetail(Integer.parseInt(goodsId),  batchInfo);
    }
}
